<?php

echo json_encode(getAAnewTotalLst());


   function getAAnewTotalLst(){
	$data=[];
	$server="localhost"; 
	$database="HPTsoft"; 
	$username="sa"; 
	$password="123456"; 
	$connstr = "Driver={SQL Server};Server=$server;Database=$database";    
	$link = odbc_connect($connstr,$username,$password,SQL_CUR_USE_ODBC);
	if ( !$link ) 
		die( "Couldn't connect to SQL Server on $server"  );
	//外部数据提供者1.查询SQL ＝ “SELECT Name,早餐补贴金额,午餐补贴金额,晚餐补贴金额,夜宵补贴金额 FROM AA部门补贴标准”
	
	//外部数据库1.执行 (“DROP TABLE AAnewTotalLst;”, )
	//外部数据库1.执行 (“SELECT *,CAST(0 AS DECIMAL(10, 2)) AS 早餐补贴金额 ,CAST(0 AS DECIMAL(10, 2)) AS 午餐补贴金额 ,CAST(0 AS DECIMAL(10, 2)) AS 晚餐补贴金额 ,CAST(0 AS DECIMAL(10, 2)) AS 夜宵补贴金额 ,CAST(0 AS DECIMAL(10, 2)) AS 补贴总金额 INTO AAnewTotalLst FROM TotalLst;”, )
	//输出调试文本 (“create AAnewTotalLst;”)
	//外部数据库1.查询 (“SELECT * FROM  AA部门补贴标准;”)
	/*
    sql ＝ “”
    sql ＝ sql ＋ “UPDATE AAnewTotalLst SET ”
    sql ＝ sql ＋ “早餐补贴金额 = 早餐消费次数 * ” ＋ 到文本 (到数值 (外部数据库1.读 (jlj, “早餐补贴金额”))) ＋ “ , ”
    sql ＝ sql ＋ “午餐补贴金额 = 午餐消费次数 * ” ＋ 到文本 (到数值 (外部数据库1.读 (jlj, “午餐补贴金额”))) ＋ “ , ”
    sql ＝ sql ＋ “晚餐补贴金额 = 晚餐消费次数 * ” ＋ 到文本 (到数值 (外部数据库1.读 (jlj, “晚餐补贴金额”))) ＋ “ , ”
    sql ＝ sql ＋ “夜宵补贴金额 = 夜宵消费次数 * ” ＋ 到文本 (到数值 (外部数据库1.读 (jlj, “夜宵补贴金额”))) ＋ “ ”
    sql ＝ sql ＋ “where 部门名称='” ＋ 到文本 (外部数据库1.读 (jlj, “Name”)) ＋ “'  ”
	*/
	/*
sql ＝ “UPDATE AAnewTotalLst SET ”
sql ＝ sql ＋ “早餐补贴金额 = (SELECT SUM(早餐补贴金额) FROM AAnewTotalLst WHERE [部门名称] <> '合计：'),”
sql ＝ sql ＋ “午餐补贴金额 = (SELECT SUM(午餐补贴金额) FROM AAnewTotalLst WHERE [部门名称] <> '合计：'),”
sql ＝ sql ＋ “晚餐补贴金额 = (SELECT SUM(晚餐补贴金额) FROM AAnewTotalLst WHERE [部门名称] <> '合计：'),”
sql ＝ sql ＋ “夜宵补贴金额 = (SELECT SUM(夜宵补贴金额) FROM AAnewTotalLst WHERE [部门名称] <> '合计：')”
sql ＝ sql ＋ “WHERE [部门名称] = '合计：';”
	*/
	//外部数据库1.执行 (“UPDATE AAnewTotalLst SET 补贴总金额 = 早餐补贴金额 + 午餐补贴金额 + 晚餐补贴金额 + 夜宵补贴金额 ;”, )


	$sql="SELECT 部门名称,早餐消费次数,早餐补贴金额,午餐消费次数,午餐补贴金额,晚餐消费次数,晚餐补贴金额,消费总金额,补贴总金额 FROM AAnewTotalLst";  
	$exec = odbc_exec($link,$sql);
	while( $row =odbc_fetch_array($exec) ){
		$data[]= ($row) ;
	}
	odbc_close( $link );
	$data = eval('return '. iconv( "GBK" , "UTF-8" , var_export($data,true).";" ) );
	return $data;
   }

/*
class Sql{
    var $err;
    var $conn;
    var $rs;
    function __construct($password="",$database="master",$username="sa",$server="localhost"){
        connect($password,$database,$username,$server);
    }
    function connect($password="",$database="master",$username="sa",$server="localhost"){
        try{
            $like = odbc_connect(
                        "Driver={SQL Server};Server=$server;Database=$database",
                        $username,
                        $password,
                        SQL_CUR_USE_ODBC
                    );
            
            if($like){
                $this -> conn = $like;
                return $this;
            }else{
                throw new Exception( 'connect[$like]: Couldn\'t connect to SQL Server .' );
                
            }
        }catch (Exception $e){
            $this -> err = $e->getMessage();
            die();
        }
        
    }
    function query($query){
        $like = $this -> conn ;
        try{
            if($like){
                $trs  = odbc_exec( $like , $query );
                if (!$trs){
                    throw new Exception( 'query[$trs]: SQL Query statement error.' );
                }else{
                    $this -> rs = $trs ;
                }
                return $this ;
            }else{
                throw new Exception( 'query[$like]: Couldn\'t connect to SQL Server .' );
            }
        }catch (Exception $e){
            $this -> err = $e->getMessage();
            die();
        }  
    }
    function fetchAll(){
        $rs = $this -> rs ;
        try{
            if($rs){
                $data = [];
            	while( $row =odbc_fetch_array($rs) ){
            		$data[]= ($row) ;
            	}
                return $data ;
            }else{
                throw new Exception( 'fetchAll[$rs]: The result set does not exist .' );
            }
        }catch (Exception $e){
            $this -> err = $e->getMessage();
            die();
        }  
    }
    function close(){
        $like = $this -> conn ;
        try{
            if($like){
                odbc_close( $like );
                return $this ;
            }else{
                throw new Exception( 'close: Couldn\'t connect to SQL Server .' );
            }
        }catch (Exception $e){
            $this -> err = $e->getMessage();
            die();
        }  
    }
    static function g2u($arr){
        return eval('return '. iconv( "GBK" , "UTF-8" , var_export($arr,true).";" ) );
    }
}
$sql = new Sql();
$sql = $sql ->connect("123456","HPTsoft");
$data = $sql ->query("SELECT 部门名称,早餐消费次数,早餐补贴金额,午餐消费次数,午餐补贴金额,晚餐消费次数,晚餐补贴金额,消费总金额,补贴总金额 FROM AAnewTotalLst") ->fetchAll();
$data = Sql::g2u($data);
*/

/*
class Dbcon{
    var $var1;
    var $var2=123;
    
    function set($var){
        $this -> var1 = $var;
    }
    function get(){
        return $this -> var2;
    }
    static function add($var){
        return 123 + $var;
    }
}

$a = new Dbcon();
$a -> set(789);
$q = $a -> get();
echo Dbcon::add($q);
*/

?>